package b.p.b;

import android.annotation.SuppressLint;
import android.os.Trace;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.e;
import androidx.recyclerview.widget.RecyclerView.m;
import androidx.recyclerview.widget.RecyclerView.s;
import androidx.recyclerview.widget.RecyclerView.w;
import androidx.recyclerview.widget.RecyclerView.z;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.TimeUnit;

/* compiled from: GapWorker */
public final class p implements Runnable {
    public static final ThreadLocal<p> e = new ThreadLocal();
    public static Comparator<c> f = new a();
    public ArrayList<RecyclerView> a = new ArrayList();
    public long b;
    public long c;
    public ArrayList<c> d = new ArrayList();

    /* compiled from: GapWorker */
    public static class a implements Comparator<c> {
        /* JADX WARNING: Missing block: B:10:0x0017, code skipped:
            if (r0 == null) goto L_0x0023;
     */
        /* JADX WARNING: Missing block: B:13:0x0020, code skipped:
            if (r0 != false) goto L_0x0022;
     */
        public int compare(java.lang.Object r7, java.lang.Object r8) {
            /*
            r6 = this;
            r7 = (b.p.b.p.c) r7;
            r8 = (b.p.b.p.c) r8;
            r0 = r7.d;
            r1 = 1;
            r2 = 0;
            if (r0 != 0) goto L_0x000c;
        L_0x000a:
            r3 = 1;
            goto L_0x000d;
        L_0x000c:
            r3 = 0;
        L_0x000d:
            r4 = r8.d;
            if (r4 != 0) goto L_0x0013;
        L_0x0011:
            r4 = 1;
            goto L_0x0014;
        L_0x0013:
            r4 = 0;
        L_0x0014:
            r5 = -1;
            if (r3 == r4) goto L_0x001a;
        L_0x0017:
            if (r0 != 0) goto L_0x0022;
        L_0x0019:
            goto L_0x0023;
        L_0x001a:
            r0 = r7.a;
            r3 = r8.a;
            if (r0 == r3) goto L_0x0025;
        L_0x0020:
            if (r0 == 0) goto L_0x0023;
        L_0x0022:
            r1 = -1;
        L_0x0023:
            r2 = r1;
            goto L_0x0036;
        L_0x0025:
            r0 = r8.b;
            r1 = r7.b;
            r0 = r0 - r1;
            if (r0 == 0) goto L_0x002e;
        L_0x002c:
            r2 = r0;
            goto L_0x0036;
        L_0x002e:
            r7 = r7.c;
            r8 = r8.c;
            r7 = r7 - r8;
            if (r7 == 0) goto L_0x0036;
        L_0x0035:
            r2 = r7;
        L_0x0036:
            return r2;
            */
            throw new UnsupportedOperationException("Method not decompiled: b.p.b.p$a.compare(java.lang.Object, java.lang.Object):int");
        }
    }

    /* compiled from: GapWorker */
    public static class c {
        public boolean a;
        public int b;
        public int c;
        public RecyclerView d;
        public int e;
    }

    @SuppressLint({"VisibleForTests"})
    /* compiled from: GapWorker */
    public static class b implements androidx.recyclerview.widget.RecyclerView.m.c {
        public int a;
        public int b;
        public int[] c;
        public int d;

        public void a(int i, int i2) {
            if (i < 0) {
                throw new IllegalArgumentException("Layout positions must be non-negative");
            } else if (i2 >= 0) {
                int i3 = this.d * 2;
                int[] iArr = this.c;
                if (iArr == null) {
                    iArr = new int[4];
                    this.c = iArr;
                    Arrays.fill(iArr, -1);
                } else if (i3 >= iArr.length) {
                    int[] iArr2 = new int[(i3 * 2)];
                    this.c = iArr2;
                    System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
                }
                iArr = this.c;
                iArr[i3] = i;
                iArr[i3 + 1] = i2;
                this.d++;
            } else {
                throw new IllegalArgumentException("Pixel distance must be non-negative");
            }
        }

        public void b(RecyclerView recyclerView, boolean z) {
            this.d = 0;
            int[] iArr = this.c;
            if (iArr != null) {
                Arrays.fill(iArr, -1);
            }
            m mVar = recyclerView.m;
            if (recyclerView.l != null && mVar != null && mVar.k) {
                if (z) {
                    if (!recyclerView.d.g()) {
                        mVar.l(recyclerView.l.a(), this);
                    }
                } else if (!recyclerView.N()) {
                    mVar.k(this.a, this.b, recyclerView.i0, this);
                }
                int i = this.d;
                if (i > mVar.l) {
                    mVar.l = i;
                    mVar.m = z;
                    recyclerView.b.l();
                }
            }
        }

        public boolean c(int i) {
            if (this.c != null) {
                int i2 = this.d * 2;
                for (int i3 = 0; i3 < i2; i3 += 2) {
                    if (this.c[i3] == i) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    public void a(RecyclerView recyclerView, int i, int i2) {
        if (recyclerView.isAttachedToWindow() && this.b == 0) {
            this.b = recyclerView.getNanoTime();
            recyclerView.post(this);
        }
        b bVar = recyclerView.h0;
        bVar.a = i;
        bVar.b = i2;
    }

    public void b(long j) {
        int i;
        int i2;
        int size = this.a.size();
        int i3 = 0;
        for (i = 0; i < size; i++) {
            RecyclerView recyclerView = (RecyclerView) this.a.get(i);
            if (recyclerView.getWindowVisibility() == 0) {
                recyclerView.h0.b(recyclerView, false);
                i3 += recyclerView.h0.d;
            }
        }
        this.d.ensureCapacity(i3);
        i3 = 0;
        for (i = 0; i < size; i++) {
            RecyclerView recyclerView2 = (RecyclerView) this.a.get(i);
            if (recyclerView2.getWindowVisibility() == 0) {
                b bVar = recyclerView2.h0;
                int abs = Math.abs(bVar.b) + Math.abs(bVar.a);
                for (i2 = 0; i2 < bVar.d * 2; i2 += 2) {
                    c cVar;
                    if (i3 >= this.d.size()) {
                        cVar = new c();
                        this.d.add(cVar);
                    } else {
                        cVar = (c) this.d.get(i3);
                    }
                    int[] iArr = bVar.c;
                    int i4 = iArr[i2 + 1];
                    cVar.a = i4 <= abs;
                    cVar.b = abs;
                    cVar.c = i4;
                    cVar.d = recyclerView2;
                    cVar.e = iArr[i2];
                    i3++;
                }
            }
        }
        Collections.sort(this.d, f);
        size = 0;
        while (size < this.d.size()) {
            c cVar2 = (c) this.d.get(size);
            RecyclerView recyclerView3 = cVar2.d;
            if (recyclerView3 != null) {
                z c = c(recyclerView3, cVar2.e, cVar2.a ? Long.MAX_VALUE : j);
                if (!(c == null || c.b == null || !c.j() || c.k())) {
                    recyclerView3 = (RecyclerView) c.b.get();
                    if (recyclerView3 != null) {
                        if (recyclerView3.D && recyclerView3.e.h() != 0) {
                            recyclerView3.e0();
                        }
                        b bVar2 = recyclerView3.h0;
                        bVar2.b(recyclerView3, true);
                        if (bVar2.d != 0) {
                            try {
                                i2 = b.i.g.b.a;
                                Trace.beginSection("RV Nested Prefetch");
                                w wVar = recyclerView3.i0;
                                e eVar = recyclerView3.l;
                                wVar.d = 1;
                                wVar.e = eVar.a();
                                wVar.g = false;
                                wVar.h = false;
                                wVar.i = false;
                                for (int i5 = 0; i5 < bVar2.d * 2; i5 += 2) {
                                    c(recyclerView3, bVar2.c[i5], j);
                                }
                                Trace.endSection();
                            } catch (Throwable th) {
                                int i6 = b.i.g.b.a;
                                Trace.endSection();
                            }
                        }
                    }
                }
                cVar2.a = false;
                cVar2.b = 0;
                cVar2.c = 0;
                cVar2.d = null;
                cVar2.e = 0;
                size++;
            } else {
                return;
            }
        }
    }

    public final z c(RecyclerView recyclerView, int i, long j) {
        Object obj;
        int h = recyclerView.e.h();
        for (int i2 = 0; i2 < h; i2++) {
            z L = RecyclerView.L(recyclerView.e.g(i2));
            if (L.c == i && !L.k()) {
                obj = 1;
                break;
            }
        }
        obj = null;
        if (obj != null) {
            return null;
        }
        s sVar = recyclerView.b;
        try {
            recyclerView.V();
            z j2 = sVar.j(i, false, j);
            if (j2 != null) {
                if (!j2.j() || j2.k()) {
                    sVar.a(j2, false);
                } else {
                    sVar.g(j2.a);
                }
            }
            recyclerView.W(false);
            return j2;
        } catch (Throwable th) {
            recyclerView.W(false);
        }
    }

    public void run() {
        try {
            int i = b.i.g.b.a;
            Trace.beginSection("RV Prefetch");
            if (this.a.isEmpty()) {
                this.b = 0;
                Trace.endSection();
                return;
            }
            int size = this.a.size();
            long j = 0;
            for (i = 0; i < size; i++) {
                RecyclerView recyclerView = (RecyclerView) this.a.get(i);
                if (recyclerView.getWindowVisibility() == 0) {
                    j = Math.max(recyclerView.getDrawingTime(), j);
                }
            }
            if (j == 0) {
                this.b = 0;
                Trace.endSection();
                return;
            }
            b(TimeUnit.MILLISECONDS.toNanos(j) + this.c);
            this.b = 0;
            Trace.endSection();
        } catch (Throwable th) {
            this.b = 0;
            int i2 = b.i.g.b.a;
            Trace.endSection();
        }
    }
}
